//
//  WDPersonalSettingTopView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/10.
//

#import "WDPersonalSettingTopView.h"

@interface WDPersonalSettingTopView ()
@property (nonatomic, strong) UIView *headerBgView;

@property (nonatomic, strong) UIImageView *headerImgView;

@property (nonatomic, strong) UIView *photoBgView;

@property (nonatomic, strong) UIImageView *photoImgView;

@end

@implementation WDPersonalSettingTopView

- (void)initWithSubView {
    [super initWithSubView];
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self initWithHeaderBgView];
    
    [self initWithHeaderImgView];
    
    [self initWithPhotoBgView];
    
    [self initWithPhotoImgView];
}

- (void)initWithHeaderBgView {
    if (!_headerBgView) {
        CGRect frame = CGRectMake((self.width - 120) * 0.5, 24, 120, 120);
        _headerBgView = [[UIView alloc] initWithFrame:frame];
        _headerBgView.backgroundColor = [UIColor whiteColor];
        _headerBgView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1600].CGColor;
        _headerBgView.layer.shadowOffset = CGSizeMake(0,3);
        _headerBgView.layer.shadowOpacity = 1;
        _headerBgView.layer.shadowRadius = 6;
        _headerBgView.layer.cornerRadius = frame.size.height * 0.5;
        [self addSubview:_headerBgView];
    }
}

- (void)initWithHeaderImgView {
    if (!_headerImgView) {
        CGRect frame = _headerBgView.bounds;
        frame.size.width = self.headerBgView.width - 4;
        frame.size.height = self.headerBgView.height - 4;
        frame.origin.x = 2;
        frame.origin.y = 2;
        _headerImgView = [UIImageView createWithImageView:self.headerBgView frame:frame defaultImage:nil propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.layer.cornerRadius = frame.size.height * 0.5;
            imageView.layer.masksToBounds = YES;
//            imageView.backgroundColor = [UIColor orangeColor];
        }];
    }
}

- (void)initWithPhotoBgView {
    if (!_photoBgView) {
        CGRect frame = CGRectMake(self.headerBgView.right - 38, self.headerBgView.bottom - 38, 38, 38);
        _photoBgView = [[UIView alloc] initWithFrame:frame];
        _photoBgView.backgroundColor = [UIColor whiteColor];
        _photoBgView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1600].CGColor;
        _photoBgView.layer.shadowOffset = CGSizeMake(0,3);
        _photoBgView.layer.shadowOpacity = 1;
        _photoBgView.layer.shadowRadius = 6;
        _photoBgView.layer.cornerRadius = frame.size.height * 0.5;
        [self addSubview:_photoBgView];
        
        @weakify(self);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            [self actionWithUploadHeader];
        }];
        [self.photoBgView addGestureRecognizer:tap];
    }
}

- (void)initWithPhotoImgView {
    if (!_photoImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_mine_setting_camera"];
        CGRect frame = CGRectMake((self.photoBgView.width - image.size.width) * 0.5, (self.photoBgView.height - image.size.height) * 0.5, image.size.width, image.size.height);
        _photoImgView = [UIImageView createWithImageView:self.photoBgView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.userInteractionEnabled = YES;
        }];
    }
}

/// 上传照片
- (void)actionWithUploadHeader {
    if (self.touchHeaderBlock) {
        self.touchHeaderBlock();
    }
}

- (void)updateWithViewData:(id)data {
    NSString *avatar = [EDSAppDelegateTool sharedInstance].userInfoModel.avatar;
    [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage imageNamed:@"ic_default_head_portrait"]];
}

@end
